﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Travan.ServerServices;
using Travan.ServerServices.Interfaces;
using TravianAutoQueue.DomainModel;
using TravianMiniBot.Entities;
using TravianMiniBot.ViewData;

namespace TravianMiniBot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var HttpHelper = new HttpHelpers();
            TravianHelper = new TravianHelper() { HttpHelper = HttpHelper };

            VillageService = new VillageService() {Httphelper = HttpHelper, TravianHelper = TravianHelper};
            
            UIServices = new UIServices();
            QueueService = new QueueService();
            Queue = new List<QueueItem>();

            dataGridView3.AutoGenerateColumns = false;
        }

        public IVillageService VillageService { get; set; }
        public ITravianHelpers TravianHelper { get; set; }
        public UIServices UIServices { get; set; }
        public List<QueueItem> Queue { get; set; }
        public QueueService QueueService { get; set; }


        private void Form1_Load(object sender, EventArgs e)
        {
            //Bind Villages to the dropdown'
            try
            {
                TravianHelper.Login(Program.session);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            
            Program.currentaccount.Villages = VillageService.GetVillages(Program.session);

            foreach (var village in Program.currentaccount.Villages)
            {
                try
                {
                    village.Buildings = VillageService.GetResourcePlateLevels(village.TravianId, Program.session);
                }
                catch (Exception ex)
                {

                }
            }
            ddlVillages.DataSource = UIServices.ConvertvillageForDropdownLists(Program.currentaccount.Villages);
            ddlVillages.DisplayMember = "Text";

            
        }

        private void ddlVillages_SelectedIndexChanged(object sender, EventArgs e)
        {
            var currentvillage = ((VillageDropdownItem) ddlVillages.SelectedItem).Village;

            dataGridView1.DataSource = currentvillage.Buildings;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["UpgradeButton"].Index)
                 return;

            var building = (VillageBuilding) dataGridView1.Rows[e.RowIndex].DataBoundItem;
            Village village = ((VillageDropdownItem)ddlVillages.SelectedItem).Village;

            QueueService.AddToQueue(Queue, new UpgradeAction(building.SlotNumber, building.BuildingType, village, VillageService));

            dataGridView3.DataSource = null;
            dataGridView3.DataSource = UIServices.ConvertToQueueGridItem(Queue);

        }


    }
}
